package com.yifi.common.utils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;

import com.yifi.common.model.MenuModel;


public class ListUtils{
    /**
     * 从列表中找出包括Value值的列表
     * @param list
     * @param keyName
     * @param value
     * @return
     */
    public static List<Map<String,Object>> findDataFromListMap(List<Map<String,Object>> list,String keyName,Object value) {

        Predicate<Map<String,Object>>predicate = new Predicate<Map<String,Object>>() {

            @Override
            public boolean evaluate(Map<String,Object> map) {
                // TODO Auto-generated method stub
                try {
                    Object val = map.get(keyName);
                    if(value == null || val == null)
                        return false;

                    return val.equals(value);
                }
                catch (Exception e) {
                    return false;
                }
            }
        };

        List<Map<String,Object>> result = (List<Map<String,Object>>) CollectionUtils.select( list, predicate);
        return result;
    }

    /**
     * 多值将交叉查找
     * @param list
     * @param keyName2ValueMap
     * @return
     */
    public static List<Map<String,Object>> findDataFromListMapByKeyValues(
            List<Map<String,Object>> list,
            Map<String,Object> keyName2ValueMap) {

        Predicate<Map<String,Object>>predicate = new Predicate<Map<String,Object>>() {

            @Override
            public boolean evaluate(Map<String,Object> map) {
                // TODO Auto-generated method stub
                try {
                    for(Map.Entry<String,Object> entry : keyName2ValueMap.entrySet()){
                        String keyName  = entry.getKey();
                        Object value = entry.getValue();
                        Object val = map.get(keyName);
                        if(StringUtils.isBlank(keyName) || value == null || val == null)
                            return false;
                        if(!val.equals(value))
                            return  false;
                    }
                    return true;
                }
                catch (Exception e) {
                    return false;
                }
            }
        };

        List<Map<String,Object>> result = (List<Map<String,Object>>) CollectionUtils.select( list, predicate);
        return result;
    }
	
    /**
     * 从列表中找出包括Value值的对象列表
     * @param list
     * @param methodName
     * @param value
     * @return
     */
    public static <T,V> List<T> findObjFromList(List<T> list,String methodName,V value) {
    	
    	Predicate<T>predicate = new Predicate<T>() {

			@Override
			public boolean evaluate(T obj) {
				// TODO Auto-generated method stub
				try {
					Method method = obj.getClass().getMethod(methodName);
					@SuppressWarnings("unchecked")
					V keyName = (V) method.invoke(obj);
					return keyName.equals(value);
				}
				catch (Exception e) {
					return false;
				}
			}
		};
		
		List<T> result = (List<T>) CollectionUtils.select( list, predicate);
		return result;
    }
    
    public static List<MenuModel> findModelFromListByPid(List<MenuModel> list,String value) {
    	List<MenuModel> listc=new ArrayList<MenuModel>();
    	for (MenuModel menuModel : list) {
			if(menuModel.getPid().equals(value))
			{
				listc.add(menuModel);
			}
		}
    	return listc;
    }
    public static List<MenuModel> findModelFromListById(List<MenuModel> list,String value) {
    	List<MenuModel> listc=new ArrayList<MenuModel>();
    	for (MenuModel menuModel : list) {
			if(menuModel.getId().equals(value))
			{
				listc.add(menuModel);
			}
		}
    	return listc;
    }
}
